#coding:utf-8

from . import db
from . import loginManager
from flask_login import UserMixin, AnonymousUserMixin
from werkzeug.security import generate_password_hash, check_password_hash

class User(UserMixin,db.Model):
    __tablename__ = 'users'
    id = db.Column(db.Integer(),primary_key=True)
    xh = db.Column(db.Integer(),unique=True)
    username = db.Column(db.String(32))
    password = db.Column(db.String(128))
    administrator = db.Column(db.Boolean(),default=False)

    def __init__(self,xh,username,password,administrator):
        self.xh = xh
        self.username = username
        self.password = generate_password_hash(password)
        self.administrator = administrator

    def isAdmin(self):
        return self.administrator

    def verify_password(self,password):
        return check_password_hash(self.password,password)


# 将 User模型 和 loginManager登录管理　关联
@loginManager.user_loader
def user_loader(user_id):
    return User.query.get(int(user_id))

class AnonymousUser(AnonymousUserMixin):
    def __init__(self):
        self.username = 'Guest'

    def isAdmin(self):
        return False
loginManager.anonymous_user = AnonymousUser     #将 AnonymousUser模型 和 loginManager登录管理　关联

class Task(db.Model):
    __tablename__ = 'tasks'
    id = db.Column(db.Integer(),primary_key=True)
    task_name = db.Column(db.String(64),unique=True)
    task_description = db.Column(db.String(256))
    task_deadline = db.Column(db.DateTime())

    def __init__(self,name,description,deadline):
        self.task_name = name
        self.task_description = description
        self.task_deadline = deadline
        